package top.eggcode.modules.user.store;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import top.eggcode.component.query.CriteriaBuilder;
import top.eggcode.component.query.model.PageList;
import top.eggcode.modules.user.model.RoleEntity;
import top.eggcode.modules.user.model.RoleQuery;
import top.eggcode.base.JpaSimpleStore;

import javax.annotation.Resource;
import javax.persistence.EntityManager;

/**
 * Title:
 * Description: TODO
 * Date: 2021/9/17 22:17
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Repository
public class RoleStore extends JpaSimpleStore<RoleEntity, String> {

    @Resource
    private CriteriaBuilder builder;

    public RoleStore(EntityManager manager) {
        super(RoleEntity.class, manager);
    }

    public PageList<RoleEntity> list(RoleQuery query) {
        return builder.createQuery(RoleEntity.class)
                .fromEntity()
                .where(condition -> {
                    if (StringUtils.isNotBlank(query.getName())) {
                        condition.likeAny("name", query.getName());
                    }
                    if (StringUtils.isNotBlank(query.getCode())) {
                        condition.likeAny("name", query.getCode());
                    }
                })
                .fetchPage(query);
    }
}
